<template>
  <div class="container">
    <div class="app-container">
      <el-card>
        <!-- 头部搜索区域 -->
        <el-row :span="24">
          <el-form :inline="true" :model="page" class="demo-form-inline">
            <el-form-item label="目录名称">
              <el-input v-model="page.directoryName"></el-input>
            </el-form-item>
            <el-form-item label="状态" style="margin-left: 40px">
              <el-select v-model="page.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="resetForm" plain>清除</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
            <el-button
              type="success"
              icon="el-icon-edit"
              style="float: right; padding: 10px"
              @click="centerDialogVisible = true"
              >新增目录</el-button
            >
          </el-form>
        </el-row>
        <!-- 提示 -->
        <el-alert type="info" show-icon style="margin-bottom: 15px">
          <template slot="title">一共{{ total }}条数据</template>
        </el-alert>
        <!-- 目录列表 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="subjectName" label="所属学科">
          </el-table-column>
          <el-table-column prop="directoryName" label="目录名称">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期">
            <template slot-scope="{ row }">
              {{ row.addDate | parseTimeByString }}
            </template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量"> </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="{ row }">
              {{ row.state === 1 ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="{ row }">
              <el-button
                @click="changeState({ id: row.id, state: row.state })"
                type="text"
                size="small"
                >{{ row.state === 1 ? "禁用" : "启用" }}</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="modify(row)"
                :disabled="row.state === 1"
                >修改</el-button
              >
              <el-button
                type="text"
                size="small"
                @click="removeUser(row.id)"
                :disabled="row.state === 1 || row.totals !== 0"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          background
          style="text-align: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="page.pagesize"
          layout="sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-card>
      <!-- 新增弹出框 -->
      <el-dialog
        :title="titleText"
        :visible="centerDialogVisible"
        @close="btnIsDid"
        width="30%"
      >
        <el-form ref="form" :model="ruleForm" :rules="rules" label-width="80px">
          <el-form-item label="所属学科" prop="subjectID">
            <el-select
              v-model="ruleForm.subjectID"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in xuelist"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
            <el-input
              placeholder="请输入目录名称"
              v-model="ruleForm.directoryName"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="btnIsDid">取 消</el-button>
          <el-button type="primary" @click="btnOk">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { list, remove, add, update, changeState, detail } from '@/api/hmmm/directorys.js'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      page: {
        page: 1, // 当前页码
        pagesize: 10, // 每页多少条
        directoryName: null, // 搜索文本框内容
        state: null // 搜索状态
      },
      total: 5, // 总数
      // formInline: {
      //   directoryName: null,
      //   state: null
      // },
      tableData: [], // 接收新增数组
      testarr: {},
      centerDialogVisible: false, // 控制弹层显示与隐藏
      centerDialog: false,
      ruleForm: {
        subjectID: '',
        directoryName: ''
      },
      xuelist: [], // 下拉列表
      rules: { // 验证规则
        subjectID: [ // 验证下拉列表
          { required: true, message: '请选择', trigger: '' }
        ],
        directoryName: [ // 验证搜索内容
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ]
      },
      title: null // 弹框的名字
    }
  },
  computed: {
    titleText () {
      return this.ruleForm.id ? '修改目录' : '新增目录'
    }
  },
  created () {
    this.list()
    this.simple()
  },
  methods: {
    // 获取列表
    async list () {
      const { data } = await list({ ...this.page })
      this.tableData = data.items
      this.total = data.counts
    },
    // 控制每页多少条
    handleSizeChange (val) {
      this.page.pagesize = val
      this.list()
    },
    // 控制当前页码
    handleCurrentChange (val) {
      this.page.page = val
      this.list()
    },
    // 重置
    resetForm () {
      this.page = {
        page: 1, // 当前页码
        pagesize: 8, // 每页多少条
        directoryName: null,
        state: null
      }
      this.list()
    },
    // 删除
    removeUser (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: id })
          .then(response => {
            this.$message.success({ message: '删除成功!' })
            this.list()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 搜索
    async search () {
      this.page.page = null
      await this.list()
    },
    // 学科列表
    async simple () {
      const { data } = await simple()
      this.xuelist = data
    },
    // 新增弹层确认添加
    btnOk () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            await update(this.ruleForm)
          } else {
            await add(this.ruleForm)
          }
          this.list()
          this.$refs.form.resetFields()
          this.centerDialogVisible = false
          this.ruleForm = {
            subjectID: '',
            directoryName: ''
          }
          this.$message.success('操作成功')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    btnIsDid () { // 新增
      this.centerDialogVisible = false
      this.$refs.form.resetFields()
      this.id = null
    },
    // 修改弹层
    async modify (row) {
      const data = await detail(row)
      this.ruleForm = data.data
      this.centerDialogVisible = true
    },
    // 权限控制
    async changeState (obj) {
      if (obj.state) {
        obj.state = 0
      } else {
        obj.state = 1
      }
      await changeState(obj)
      this.list()
    }
  }
}
</script>
<style scoped lang='less'></style>
